package com.psp.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.web.controller.res.ListResult;
import com.psp.web.controller.res.ObjectResult;
import com.psp.web.controller.res.bean.RAttributeBean;
import com.psp.web.controller.res.bean.RBusinessCommentsBean;
import com.psp.web.controller.res.bean.RBusinessDetailBean;
import com.psp.web.controller.res.bean.RProductListBean;
import com.psp.web.controller.res.bean.RProductSkuBean;
import com.psp.web.controller.res.bean.RServicesMenusBean;
import com.psp.web.controller.res.bean.RServicesTypeBean;
import com.psp.web.controller.res.bean.RSimilarProductBean;
import com.psp.web.controller.springmvc.req.GetBusinessCommentsParam;
import com.psp.web.controller.springmvc.req.GetBusinessDetailParam;
import com.psp.web.controller.springmvc.req.GetProductAttributesParam;
import com.psp.web.controller.springmvc.req.GetProductInfoBySkunoParam;
import com.psp.web.controller.springmvc.req.GetProductListParam;
import com.psp.web.controller.springmvc.req.GetServiceMenusParam;
import com.psp.web.controller.springmvc.req.GetServiceTypeParam;
import com.psp.web.controller.springmvc.req.GetSimilarProductParam;
import com.psp.web.service.ProductService;
import com.psp.web.service.exception.ServiceException;
import com.psp.web.service.impl.PageResult;

/**
 * 服务
 */
@Component
public class BusinessController {
	Logger logger = Logger.getLogger(BusinessController.class);

	@Autowired
	ProductService productServiceImpl;

	public ObjectResult<RServicesTypeBean> getTypes(GetServiceTypeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RServicesTypeBean> result = new ObjectResult<>();
		try {
			RServicesTypeBean bean = productServiceImpl.getServiceTypes();
			if (bean != null) {
				result.setData(bean);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 获取服务列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProductListBean> getLists(GetProductListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProductListBean> result = new ListResult<>();
		try {
			int firstType = NumUtil.toInt(param.getFirstType(), 0);
			int secType = NumUtil.toInt(param.getSecType(), 0);
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			PageResult<RProductListBean> resLists = productServiceImpl.getLists(page, pageSize, firstType, secType);
			if (resLists != null) {
				result.setData(resLists.getData());
				result.setTotalSize(resLists.getCount());
				result.setPage(page);
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		return result;
	}

	/**
	 * 获取服务详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RBusinessDetailBean> getDetail(GetBusinessDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RBusinessDetailBean> result = new ObjectResult<>();
		try {
			String pid = param.getPid();
			RBusinessDetailBean bean = productServiceImpl.getDetail(pid);
			result.setData(bean);
			result.setFlag(bean != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		return result;
	}

	/**
	 * 服务的评论信息列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RBusinessCommentsBean> getComments(GetBusinessCommentsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RBusinessCommentsBean> result = new ListResult<RBusinessCommentsBean>();

		int page = param.getPage() == null ? 0 : param.getPage();
		int pageSize = param.getPageSize() == null ? 0 : param.getPageSize();
		String bid = param.getBid();
		try {
			PageResult<RBusinessCommentsBean> resData = productServiceImpl.getComments(page, pageSize, bid);
			if (resData != null) {
				result.setData(resData.getData());
				result.setPage(page);
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 服务菜单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RServicesMenusBean> getMenus(GetServiceMenusParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RServicesMenusBean> result = new ObjectResult<>();
		try {
			RServicesMenusBean bean = productServiceImpl.getServiceMenus();
			if (bean != null) {
				result.setData(bean);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 获取属性值
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RAttributeBean> getProductAttributes(GetProductAttributesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RAttributeBean> result = new ListResult<RAttributeBean>();

		String pid = param.getPid();
		try {
			List<RAttributeBean> resData = productServiceImpl.getAttributes(pid);
			if (resData != null) {
				result.setData(resData);
				result.setTotalSize(resData.size());
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	public ObjectResult<RProductSkuBean> getProductInfoBySkuno(GetProductInfoBySkunoParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductSkuBean> result = new ObjectResult<RProductSkuBean>();

		String skuCode = param.getSkuCode();
		try {
			RProductSkuBean resData = productServiceImpl.getProductInfoBySkuno(skuCode);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}
	
	/**
	 * 获取相关服务列表信息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RSimilarProductBean> getSimilarProduct(GetSimilarProductParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RSimilarProductBean> result = new ObjectResult<RSimilarProductBean>();
		String pid = param.getPid();
		try {
			RSimilarProductBean res = productServiceImpl.getSimilarBs(pid);
			if (res != null) {
				result.setData(res);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}


}
